package com.shanghaionstar.user.presenter;

import android.content.Context;

import com.onstar.cn.framework.mvp.base.IMVPScheduler;
import com.onstar.cn.framework.mvp.base.core.BasePresenter;
import com.shanghaionstar.user.iview.IClearCacheView;
import com.shanghaionstar.user.service.CacheService;

import rx.functions.Action1;

/**
 * Created by Yonglang Hong on 2016/8/31.
 */
public class ClearCachePresenter extends BasePresenter {

    private IClearCacheView clearCacheView;

    private CacheService  cacheService;

    public ClearCachePresenter(IMVPScheduler mvpScheduler, Context context) {
        super(mvpScheduler);
        cacheService = new CacheService(context);
    }

    public void setClearCacheView(IClearCacheView clearCacheView) {
        this.clearCacheView = clearCacheView;
    }

    public void clearCache() {
        subscription = cacheService.clearCache()
                .subscribeOn(mvpScheduler.backgroundThread())
                .observeOn(mvpScheduler.mainThread())
                .subscribe(new Action1<String>() {
                    @Override
                    public void call(String response) {
                        if (clearCacheView != null) {
                            clearCacheView.onComplete(null);
                        }
                    }
                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {
                        //里面抛出的Error信息
                        if (clearCacheView != null && throwable != null) {
                            throwable.printStackTrace();
                            clearCacheView.onComplete(throwable);
                        }
                    }
                });
    }
}
